package com.yanyeori.framework.elasticjob.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yanyeori.framework.core.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

/**
 * Job调度处理分发
 *
 * @author chenkuan 2020/8/19
 */
@Slf4j
public class SimpleJobHandle implements SimpleJob {

    @Override
    public void execute(ShardingContext context) {
        log.debug("JobName: {}, shardingTotalCount: {}, shardingItem: {}, jobParameter: {}", context.getJobName(), context.getShardingTotalCount(), context.getShardingItem(), context.getJobParameter());
        Job job = null;
        try {
            job = SpringUtil.getBean(context.getJobName());
        } catch (NoSuchBeanDefinitionException e) {
            log.error("Can't find handler [{}]", context.getJobName());
        }
        if (job != null) {
            job.handle(context);
        }
    }
}
